
package robotinterface;

import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.event.*;
import robotinterface.client.RobotClient;

/**
 *
 * Nathan Hemmes / Brian Bailey
 */
public class BuildManual extends JPanel {

    ManualTab manualTab;
    JLabel jointLabel;
    JLabel maxLabel;
    JLabel minLabel;

    final JSlider jointSlider;
    final JSpinner jointSpinner;

    public BuildManual(final ManualTab manualTab, String label, int min, int max,
            String minimumLabel, String maximumLabel)
    {
        this.manualTab = manualTab;

        int init = (max + min)/2;
        int step = 1;

        SpringLayout jointLayout = new SpringLayout();
        setLayout(jointLayout);
        setBorder(BorderFactory.createEtchedBorder());

        jointLabel = new JLabel(label);
        add(jointLabel);
        jointLayout.putConstraint(SpringLayout.NORTH, jointLabel, 0, SpringLayout.NORTH, this);
        jointLayout.putConstraint(SpringLayout.WEST, jointLabel, 40, SpringLayout.WEST, this);
        jointLayout.putConstraint(SpringLayout.SOUTH, jointLabel, 0, SpringLayout.SOUTH, this);
        jointLayout.putConstraint(SpringLayout.EAST, jointLabel, -380, SpringLayout.EAST, this);

        minLabel = new JLabel(minimumLabel + "°");
        add(minLabel);
        jointLayout.putConstraint(SpringLayout.NORTH, minLabel, 8, SpringLayout.NORTH, this);
        jointLayout.putConstraint(SpringLayout.WEST, minLabel, 10, SpringLayout.EAST, jointLabel);
        jointLayout.putConstraint(SpringLayout.SOUTH, minLabel, -25, SpringLayout.SOUTH, this);
        jointLayout.putConstraint(SpringLayout.WEST, minLabel, 10, SpringLayout.EAST, jointLabel);


        int value = 0;
        int stepSize = 1;

        SpinnerNumberModel model = new SpinnerNumberModel(value, min, max, stepSize);
        jointSpinner = new JSpinner(model);
        jointSlider = new JSlider(min, max, init);
        jointSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider aSlider = (JSlider)e.getSource();
                int value = (int) aSlider.getValue();
                jointSpinner.setValue(value);
                if (!aSlider.getModel().getValueIsAdjusting())
                    manualTab.sendMessage();
            }
        });

        jointSpinner.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent c){
                JSpinner aSpinner = (JSpinner)c.getSource();
                int value = (int) aSpinner.getModel().getValue();
                if(value > jointSlider.getMaximum()){
                    value = jointSlider.getMaximum();
                    aSpinner.setValue(value);
                }else if(value < jointSlider.getMinimum()){
                    value = jointSlider.getMinimum();
                    aSpinner.setValue(value);
                }
                jointSlider.setValue( value );
            }
        });

        /* SHOULD BE ADDING AS SEPERATE PANEL */

        add(jointSpinner);
        jointLayout.putConstraint(SpringLayout.NORTH, jointSpinner, 8, SpringLayout.NORTH, this);
        //jointLayout.putConstraint(SpringLayout.WEST, jointSpinner, 82, SpringLayout.EAST, minLabel);

        jointLayout.putConstraint(SpringLayout.SOUTH, jointSpinner, 30, SpringLayout.NORTH, this);
        jointLayout.putConstraint(SpringLayout.EAST, jointSpinner, -146, SpringLayout.EAST, this);


        maxLabel = new JLabel(maximumLabel + "°");
        add(maxLabel);
        jointLayout.putConstraint(SpringLayout.NORTH, maxLabel, 8, SpringLayout.NORTH, this);
        jointLayout.putConstraint(SpringLayout.EAST, maxLabel, -60, SpringLayout.WEST, this);
        jointLayout.putConstraint(SpringLayout.SOUTH, maxLabel, -25, SpringLayout.SOUTH, this);
        jointLayout.putConstraint(SpringLayout.EAST, maxLabel, -10, SpringLayout.EAST, this);

        /* Little Better Formatting*/
        jointLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, jointSlider, 0, SpringLayout.HORIZONTAL_CENTER, jointSlider);
        jointLayout.putConstraint(SpringLayout.WEST, jointSpinner, -50, SpringLayout.HORIZONTAL_CENTER, jointSlider);

        //JSlider jointSlider = new JSlider(JSlider.HORIZONTAL, min, max, init);
        jointSlider.setPaintLabels(true);
        jointSlider.setSnapToTicks(false);
        add(jointSlider);
        jointLayout.putConstraint(SpringLayout.NORTH, jointSlider, 0, SpringLayout.SOUTH, jointSpinner);
        jointLayout.putConstraint(SpringLayout.WEST, jointSlider, 10, SpringLayout.EAST, jointLabel);
        jointLayout.putConstraint(SpringLayout.SOUTH, jointSlider, 0, SpringLayout.SOUTH, this);
        jointLayout.putConstraint(SpringLayout.EAST, jointSlider, -10, SpringLayout.EAST, this);
        /* Better Fluid Format */
        //jointLayout.putConstraint(SpringLayout.WEST, jointSlider, 70, SpringLayout.WEST, jointLabel);

    }

    public double getValue()
    {
        return jointSlider.getValue();
    }

    public void setTooltip(String panelLabel, String iconDir)
    {
        String aImage = "file:"+iconDir;
        //setToolTipText("<html> <img src=" + aImage + "></html>");
        jointLabel.setToolTipText("<html>"+"<h4><center>"+ panelLabel +
                "</center></h4></br>"+" <img src=" + aImage + "></html>");

        minLabel.setToolTipText( "Minimum range for joint: "+ panelLabel );
        maxLabel.setToolTipText( "Maximum range for joint: "+ panelLabel );
    }

    class DoubleJSlider extends JSlider {

        final int scale;


        public DoubleJSlider(int min, int max, int value, int scale) {
            super(min, max, value);
            this.scale = scale;
        }
    }

}
